package com.taiyuan.tongcheng.ui;

import org.kymjs.kjframe.ui.BindView;
import org.kymjs.kjframe.ui.KJFragment;
import org.kymjs.kjframe.utils.KJLoger;

import android.text.style.ClickableSpan;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.taiyuan.tongcheng.ui.fragment.OrderCargoFragment;
import com.taiyuan.tongcheng.ui.fragment.OrderMovingFragment;
import com.taiyuan.tongcheng.widget.OrderStylePopupWindow;
import com.tytc56.user.R;
/**
 * 
* @Description 订单管理界面
* @author Administrator  
* @date 2015-8-29 下午10:20:30 
* @update (date)
* @version V1.0
 */
public class OrderActivity extends BaseActivity{
	
//	widget 初始话
	@BindView(id=R.id.tv_order_select)
	private TextView orderTopTv;
	@BindView(id=R.id.img_select_arrow)
	private ImageView orderArrow;
	@BindView(id=R.id.ll_order_style,click=true)
	private LinearLayout  orderSelectLL ;
	@BindView(id=R.id.btn_top_back,click=true)
	private Button topBackBtn;
	
	//内容
	private KJFragment contentFramentMoving ;
	private KJFragment contentFramentCargo;
	private KJFragment currentFragment;
	
	private OrderStylePopupWindow  oderStylePw;
	private Boolean clickFlag=false;
	//TAG
	public static final String TAG_FROM="comfrom";
	public static final int TAG_FROM_CARGO = 1;
	public static final int TAG_FROM_MOVING = 2;
	private int comFrom=1;
	
	@Override
	public void setRootView() {
	  setContentView(R.layout.activity_order);
	}
	
	@Override
	public void initData() {
		super.initData();
		contentFramentMoving=new OrderMovingFragment();
		contentFramentCargo=new OrderCargoFragment();
	}
	
	@Override
	public void initWidget() {
		super.initWidget();
		oderStylePw = new  OrderStylePopupWindow(this, new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				oderStylePw.setImgOrderSelect(1);//设置
				clickFlag=!clickFlag;
				oderStylePw.dismiss();//点击消失
				orderTopTv.setText(R.string.order_moving);
				changeFragment(contentFramentMoving);
			}
		}, new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				oderStylePw.setImgOrderSelect(2);
				clickFlag=!clickFlag;
				oderStylePw.dismiss();//点击消失
				orderTopTv.setText(R.string.order_cargo);
				changeFragment(contentFramentCargo);
				
			}
		});
		comFrom=getIntent().getIntExtra(TAG_FROM, 1);
		if(comFrom==TAG_FROM_CARGO){
			changeFragment(contentFramentCargo);
			oderStylePw.setImgOrderSelect(2);
			orderTopTv.setText(R.string.order_cargo);
			
		}else if(comFrom==TAG_FROM_MOVING){
			changeFragment(contentFramentMoving);
			oderStylePw.setImgOrderSelect(1);
			orderTopTv.setText(R.string.order_moving);
		}
		
		
		oderStylePw.setTouchInterceptor(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
					oderStylePw.dismiss();
					return true;
				}
				return false;
			}
		});
	}
	
	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
		switch (v.getId()) {
		case R.id.ll_order_style:
			if(clickFlag){
				oderStylePw.dismiss();
				arrowCloseAnim();
			}else{
				oderStylePw.showAsDropDown(orderSelectLL);
				arrowOpenAnim();
			}
			clickFlag=!clickFlag;
			break;
		case R.id.btn_top_back:
			OrderActivity.this.finish();
			break;
		default:
			break;
		}
	}
	
	
	private void arrowOpenAnim() {
         //后期优化加动画
		orderArrow.setImageResource(R.drawable.icon_arrow_up);
		orderArrow.startAnimation(AnimationUtils.loadAnimation(this, R.anim.arrow_open_anim));
	}
	
	private void arrowCloseAnim(){
		  //后期优化加动画
		orderArrow.setImageResource(R.drawable.icon_arrow_down);
		orderArrow.startAnimation(AnimationUtils.loadAnimation(this, R.anim.arrow_close_anim));
	}

    public void changeFragment(KJFragment targetFragment) {
        currentFragment = targetFragment;
        KJLoger.debug(targetFragment.getClass().getName());
        super.changeFragment(R.id.main_content, targetFragment);
    }
	
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
    	  if (checkInteraptorTouch(oderStylePw, ev)) {
    		   return true;
    		  }
    	return super.dispatchTouchEvent(ev);
    }
    
    private boolean checkInteraptorTouch(PopupWindow w, MotionEvent ev) {
    	  if (w == null || ev == null || !w.isShowing()) {
    	   return false;
    	  }
    	  View v = w.getContentView();
    	  if (v == null) {
    	   return false;
    	  }
    	  //
    	  int[] xy = new int[2];
    	  v.getLocationOnScreen(xy);
    	  int tx = (int) ev.getX();
    	  int ty = (int) ev.getY();
    	  // 触摸点在对话框外围
    	  if (xy[0] < tx && xy[1] < ty && tx > xy[0] + v.getWidth()
    	    && ty > xy[1] + v.getHeight()) {
    	   return false;
    	  } else {
    	   w.dismiss();
    	   return true;

    	  }
    }
}
